package com.example.myapplication2.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import com.example.myapplication2.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ViewPagerDemo3 extends PagerAdapter {
    private Context context;
    private ArrayList<HashMap<String, Object>> list;
    private List<View> imageViewList = new ArrayList<>();

    public ViewPagerDemo3(Context context, ArrayList<HashMap<String, Object>> list) {
        this.context = context;
        this.list = list;
        for (int i = 0; i < list.size(); i++) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.pager3, null);
            ImageView imageView_pager = inflate.findViewById(R.id.imageView_pager);
            RadioGroup rg_pager = inflate.findViewById(R.id.rg_pager);
            Button button_pager = inflate.findViewById(R.id.button_pager);
            imageView_pager.setImageResource((Integer) list.get(i).get("ioc"));
            for (int i1 = 0; i1 < list.size(); i1++) {
                RadioButton radioButton=new RadioButton(context);
                radioButton.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                ));
                radioButton.setPadding(10,10,10,10);
                if (i1==i){
                    radioButton.setChecked(true);
                }
                rg_pager.addView(radioButton);
            }
            int size = list.size();
            if ((size-1)==i) {
                button_pager.setVisibility(View.VISIBLE);
                System.out.println("输出");
                button_pager.setOnClickListener(view -> {
                    Toast.makeText(context,"开始啦",Toast.LENGTH_SHORT).show();
                });
            }

            imageViewList.add(inflate);
        }
    }

    @Override
    public int getCount() {
        return list.size();
    }

    //比较两个对象是否相等,根据instantiateItem方法里面的返回值关系决定的
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    //初始化
    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View imageView = imageViewList.get(position);
        container.addView(imageView);
        return imageView;
    }

    //销毁
    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView(imageViewList.get(position));
    }
}